package com.topboya.linker.ui.presenter;

import com.topboya.linker.R;
import com.topboya.linker.api.ApiRetrofit;
import com.topboya.linker.model.response.UserDataResponse;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.view.UserDataView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;


import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class UserDataPresenter extends BasePresenter<UserDataView> {

    private static final String TAG = "UserDataPresenter";
    public UserDataPresenter(BaseActivity context) {
        super(context);
    }

    public void getUserData() {

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getUserData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(userDataResponse -> {
                    String success = userDataResponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();
                    if (null != success && Boolean.parseBoolean(success)) {

                        UserDataResponse.Data  data = userDataResponse.getData();
                        getView().getUserData(data);
                    }
                }, this::loginError);
    }

    private void loginError(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
//        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "loginError:" + throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();
    }

}
